home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / XDEMOS / XDEMO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-21  |  7.7 KB  |  345 lines

  1. /* $Id: xdemo.c,v 3.0 1998/02/21 02:16:54 brianp Exp $ */
  2.  
  3.  
  4. /*
  5.  * Very simple demo of how to use the Mesa/X11 interface instead of the
  6.  * glx, tk or aux toolkits.  I highly recommend using the GLX interface
  7.  * instead of the X/Mesa interface, however.
  8.  *
  9.  * This program is in the public domain.
  10.  *
  11.  * Brian Paul
  12.  */
  13.  
  14.  
  15. /*
  16.  * $Log: xdemo.c,v $
  17.  * Revision 3.0  1998/02/21 02:16:54  brianp
  18.  * initial rev
  19.  *
  20.  */
  21.  
  22.  
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <X11/Xlib.h>
  28. #include <X11/Xutil.h>
  29. #include "GL/xmesa.h"
  30. #include "GL/gl.h"
  31.  
  32.  
  33.  
  34. static GLint Black, Red, Green, Blue;
  35.  
  36.  
  37.  
  38. static void make_window( char *title, int color_flag )
  39. {
  40.    int x = 10, y = 10, width = 400, height = 300;
  41.    Display *dpy;
  42.    int scr;
  43.    Window root, win;
  44.    Colormap cmap;
  45.    XColor xcolor;
  46.    int attr_flags;
  47.    XVisualInfo *visinfo;
  48.    XSetWindowAttributes attr;
  49.    XTextProperty tp;
  50.    XSizeHints sh;
  51.    XEvent e;
  52.    XMesaContext context;
  53.    XMesaVisual visual;
  54.    XMesaBuffer buffer;
  55.  
  56.  
  57.    /*
  58.     * Do the usual X things to make a window.
  59.     */
  60.  
  61.    dpy = XOpenDisplay(NULL);
  62.    if (!dpy) {
  63.       printf("Couldn't open default display!\n");
  64.       exit(1);
  65.    }
  66.  
  67.    scr = DefaultScreen(dpy);
  68.    root = RootWindow(dpy, scr);
  69.  
  70.    /* alloc visinfo struct */
  71.    visinfo = (XVisualInfo *) malloc( sizeof(XVisualInfo) );
  72.  
  73.    /* Get a visual and colormap */
  74.    if (color_flag) {
  75.       /* Open TrueColor window */
  76.  
  77. /*
  78.       if (!XMatchVisualInfo( dpy, scr, 24, TrueColor, visinfo )) {
  79.      printf("Couldn't get 24-bit TrueColor visual!\n");
  80.      exit(1);
  81.       }
  82. */
  83.       if (!XMatchVisualInfo( dpy, scr, 8, PseudoColor, visinfo )) {
  84.      printf("Couldn't get 8-bit PseudoColor visual!\n");
  85.      exit(1);
  86.       }
  87.  
  88.       cmap = XCreateColormap( dpy, root, visinfo->visual, AllocNone );
  89.       Black = Red = Green = Blue = 0;
  90.    }
  91.    else {
  92.       /* Open color index window */
  93.  
  94.       if (!XMatchVisualInfo( dpy, scr, 8, PseudoColor, visinfo )) {
  95.      printf("Couldn't get 8-bit PseudoColor visual\n");
  96.      exit(1);
  97.       }
  98.  
  99.       cmap = XCreateColormap( dpy, root, visinfo->visual, AllocNone );
  100.  
  101.       /* Allocate colors */
  102.       xcolor.red   = 0x0;
  103.       xcolor.green = 0x0;
  104.       xcolor.blue  = 0x0;
  105.       xcolor.flags = DoRed | DoGreen | DoBlue;
  106.       if (!XAllocColor( dpy, cmap, &xcolor )) {
  107.      printf("Couldn't allocate black!\n");
  108.      exit(1);
  109.       }
  110.       Black = xcolor.pixel;
  111.  
  112.       xcolor.red   = 0xffff;
  113.       xcolor.green = 0x0;
  114.       xcolor.blue  = 0x0;
  115.       xcolor.flags = DoRed | DoGreen | DoBlue;
  116.       if (!XAllocColor( dpy, cmap, &xcolor )) {
  117.      printf("Couldn't allocate red!\n");
  118.      exit(1);
  119.       }
  120.       Red = xcolor.pixel;
  121.  
  122.       xcolor.red   = 0x0;
  123.       xcolor.green = 0xffff;
  124.       xcolor.blue  = 0x0;
  125.       xcolor.flags = DoRed | DoGreen | DoBlue;
  126.       if (!XAllocColor( dpy, cmap, &xcolor )) {
  127.      printf("Couldn't allocate green!\n");
  128.      exit(1);
  129.       }
  130.       Green = xcolor.pixel;
  131.  
  132.       xcolor.red   = 0x0;
  133.       xcolor.green = 0x0;
  134.       xcolor.blue  = 0xffff;
  135.       xcolor.flags = DoRed | DoGreen | DoBlue;
  136.       if (!XAllocColor( dpy, cmap, &xcolor )) {
  137.      printf("Couldn't allocate blue!\n");
  138.      exit(1);
  139.       }
  140.       Blue = xcolor.pixel;
  141.    }
  142.  
  143.    /* set window attributes */
  144.    attr.colormap = cmap;
  145.    attr.event_mask = ExposureMask | StructureNotifyMask;
  146.    attr.border_pixel = BlackPixel( dpy, scr );
  147.    attr.background_pixel = BlackPixel( dpy, scr );
  148.    attr_flags = CWColormap | CWEventMask | CWBorderPixel | CWBackPixel;
  149.  
  150.    /* Create the window */
  151.    win = XCreateWindow( dpy, root, x,y, width, height, 0,
  152.                 visinfo->depth, InputOutput,
  153.                 visinfo->visual,
  154.                 attr_flags, &attr);
  155.    if (!win) {
  156.       printf("Couldn't open window!\n");
  157.       exit(1);
  158.    }
  159.  
  160.    XStringListToTextProperty(&title, 1, &tp);
  161.    sh.flags = USPosition | USSize;
  162.    XSetWMProperties(dpy, win, &tp, &tp, 0, 0, &sh, 0, 0);
  163.    XMapWindow(dpy, win);
  164.    while (1) {
  165.       XNextEvent( dpy, &e );
  166.       if (e.type == MapNotify && e.xmap.window == win) {
  167.      break;
  168.       }
  169.    }
  170.  
  171.  
  172.    /*
  173.     * Now do the special Mesa/Xlib stuff!
  174.     */
  175.  
  176.    visual = XMesaCreateVisual( dpy, visinfo,
  177.                               (GLboolean) color_flag,
  178.                                GL_FALSE,  /* alpha_flag */
  179.                                GL_FALSE,  /* db_flag */
  180.                                GL_FALSE,  /* stereo flag */
  181.                                GL_FALSE,  /* ximage_flag */
  182.                                0,         /* depth size */
  183.                                0,         /* stencil size */
  184.                                0,         /* accum_size */
  185.                                0          /* level */
  186.                               );
  187.    if (!visual) {
  188.       printf("Couldn't create Mesa/X visual!\n");
  189.       exit(1);
  190.    }
  191.  
  192.    /* Create a Mesa rendering context */
  193.    context = XMesaCreateContext( visual,
  194.                                  NULL       /* share_list */
  195.                                );
  196.    if (!context) {
  197.       printf("Couldn't create Mesa/X context!\n");
  198.       exit(1);
  199.    }
  200.  
  201.    buffer = XMesaCreateWindowBuffer( visual, win );
  202.    if (!buffer) {
  203.       printf("Couldn't create Mesa/X buffer!\n");
  204.       exit(1);
  205.    }
  206.  
  207.  
  208.    XMesaMakeCurrent( context, buffer );
  209.  
  210.    /* Ready to render! */
  211. }
  212.  
  213.  
  214.  
  215. static void draw_cube( void )
  216. {
  217.    /* X faces */
  218.    glIndexi( Red );
  219.    glColor3f( 1.0, 0.0, 0.0 );
  220.    glBegin( GL_POLYGON );
  221.    glVertex3f( 1.0, 1.0, 1.0 );
  222.    glVertex3f( 1.0, -1.0, 1.0 );
  223.    glVertex3f( 1.0, -1.0, -1.0 );
  224.    glVertex3f( 1.0, 1.0, -1.0 );
  225.    glEnd();
  226.  
  227.    glBegin( GL_POLYGON );
  228.    glVertex3f( -1.0, 1.0, 1.0 );
  229.    glVertex3f( -1.0, 1.0, -1.0 );
  230.    glVertex3f( -1.0, -1.0, -1.0 );
  231.    glVertex3f( -1.0, -1.0, 1.0 );
  232.    glEnd();
  233.  
  234.    /* Y faces */
  235.    glIndexi( Green );
  236.    glColor3f( 0.0, 1.0, 0.0 );
  237.    glBegin( GL_POLYGON );
  238.    glVertex3f(  1.0, 1.0,  1.0 );
  239.    glVertex3f(  1.0, 1.0, -1.0 );
  240.    glVertex3f( -1.0, 1.0, -1.0 );
  241.    glVertex3f( -1.0, 1.0,  1.0 );
  242.    glEnd();
  243.  
  244.    glBegin( GL_POLYGON );
  245.    glVertex3f(  1.0, -1.0,  1.0 );
  246.    glVertex3f( -1.0, -1.0,  1.0 );
  247.    glVertex3f( -1.0, -1.0, -1.0 );
  248.    glVertex3f(  1.0, -1.0, -1.0 );
  249.    glEnd();
  250.  
  251.    /* Z faces */
  252.    glIndexi( Blue );
  253.    glColor3f( 0.0, 0.0, 1.0 );
  254.    glBegin( GL_POLYGON );
  255.    glVertex3f(  1.0,  1.0,  1.0 );
  256.    glVertex3f( -1.0,  1.0,  1.0 );
  257.    glVertex3f( -1.0, -1.0,  1.0 );
  258.    glVertex3f(  1.0, -1.0,  1.0 );
  259.    glEnd();
  260.  
  261.    glBegin( GL_POLYGON );
  262.    glVertex3f(  1.0, 1.0, -1.0 );
  263.    glVertex3f(  1.0,-1.0, -1.0 );
  264.    glVertex3f( -1.0,-1.0, -1.0 );
  265.    glVertex3f( -1.0, 1.0, -1.0 );
  266.    glEnd();
  267. }
  268.  
  269.  
  270.  
  271.  
  272. static void display_loop( void )
  273. {
  274.    GLfloat xrot, yrot, zrot;
  275.  
  276.    xrot = yrot = zrot = 0.0;
  277.  
  278.    glClearColor( 0.0, 0.0, 0.0, 0.0 );
  279.    glClearIndex( Black );
  280.  
  281.    glMatrixMode( GL_PROJECTION );
  282.    glLoadIdentity();
  283.    glFrustum( -1.0, 1.0,  -1.0, 1.0,  1.0, 10.0 );
  284.    glTranslatef( 0.0, 0.0, -5.0 );
  285.  
  286.    glMatrixMode( GL_MODELVIEW );
  287.    glLoadIdentity();
  288.  
  289.    glCullFace( GL_BACK );
  290.    glEnable( GL_CULL_FACE );
  291.  
  292.    glShadeModel( GL_FLAT );
  293.  
  294.    while (1) {
  295.       glClear( GL_COLOR_BUFFER_BIT );
  296.       glPushMatrix();
  297.       glRotatef( xrot, 1.0, 0.0, 0.0 );
  298.       glRotatef( yrot, 0.0, 1.0, 0.0 );
  299.       glRotatef( zrot, 0.0, 0.0, 1.0 );
  300.  
  301.       draw_cube();
  302.  
  303.       glPopMatrix();
  304.       glFinish();
  305.  
  306.       xrot += 10.0;
  307.       yrot += 7.0;
  308.       zrot -= 3.0;
  309.    }
  310.  
  311. }
  312.  
  313.  
  314.  
  315.  
  316. int main( int argc, char *argv[] )
  317. {
  318.    int mode = 0;
  319.  
  320.    if (argc >= 2)
  321.    {
  322.         if (strcmp(argv[1],"-ci")==0)
  323.            mode = 0;
  324.         else if (strcmp(argv[1],"-rgb")==0)
  325.            mode = 1;
  326.         else
  327.         {
  328.            printf("Bad flag: %s\n", argv[1]);
  329.            printf("Specify -ci for 8-bit color index or -rgb for RGB mode\n");
  330.            exit(1);
  331.         }
  332.    }
  333.    else
  334.    {
  335.         printf("Specify -ci for 8-bit color index or -rgb for RGB mode\n");
  336.         printf("Defaulting to  8-bit color index\n");
  337.    }
  338.  
  339.    make_window( argv[0], mode );
  340.  
  341.    display_loop();
  342.    return 0;
  343. }
  344.  
  345.